using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Custom;
using PlugNT.Cms.Model;
//5^1^a^s^p^x
namespace PlugNT.Cms.BLL
{



    public class Sys
    {

        private static readonly PlugNT.Cms.DAL.Sys dal = new PlugNT.Cms.DAL.Sys();
        private static SysInfo sysinfo = null;






        public static SysInfo Info
        {
            get
            {
                if (sysinfo == null)
                {
                    sysinfo = dal.GetSysInfo();


                    sysinfo.commentfilters = new Dictionary<string, bool>();
                    if (sysinfo.commentfilter != "")
                    {

                        sysinfo.commentfilter = PlugNT.Common.Utils.ClearSpace(sysinfo.commentfilter);
                        StringBuilder sb = new StringBuilder();
                        string[] filters = sysinfo.commentfilter.Split(',');
                        foreach (string s in filters)
                        {
                            if (!sysinfo.commentfilters.ContainsKey(s))
                            {
                                if (s.Contains("{"))
                                {
                                    sysinfo.commentfilters.Add(s.Replace("{", "(.{0,").Replace("}", "})"), true);
                                }
                                else
                                {
                                    sysinfo.commentfilters.Add(s, false);
                                }
                                sb.Append(s + ",");
                            }
                        }
                        sysinfo.commentfilter = sb.ToString().Trim(',');
                    }

                }
                return sysinfo;
            }
            set
            {
                sysinfo = value;
            }
        }






        public static bool Update()
        {
            if (sysinfo != null)
            {
                return dal.Update(sysinfo);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateArticleupdated()
        {
            if (sysinfo != null)
            {
                return dal.UpdateArticleupdated(sysinfo.id, sysinfo.articleupdated);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateCommentfilter()
        {
            if (sysinfo != null)
            {
                return dal.UpdateCommentfilter(sysinfo.id, sysinfo.commentfilter);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateUsersadminhtml()
        {
            if (sysinfo != null)
            {
                return dal.UpdateUsersadminhtml(sysinfo.id, sysinfo.usersadminhtml);
            }
            else
            {
                return false;
            }
        }







        public static bool CheckComment(params string[] args)
        {
            foreach (System.Collections.Generic.KeyValuePair<string, bool> kv in Info.commentfilters)
            {

                if (kv.Value)
                {
                    foreach (string arg in args)
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(arg, kv.Key))
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    foreach (string arg in args)
                    {
                        if (arg.Contains(kv.Key))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }


        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
